package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * Process.java
 * <p>
 * 
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0
 * @Date Jan 8, 2010
 */
public class Segment extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(Segment.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            //        String segment = this.getParamMap().get("段名");
            String serverName = this.getParamMap().get("ServerName");
            String segment = this.getParamMap().get("SegmentName");
            String sql1 = "select low from master.dbo.spt_values where number = 1 and  type = 'E'";
            String sql2 = "select tsize=sum(u.size),fsize=sum(u.unreservedpgs) from syssegments s,master..sysusages u " + ", master..sysdatabases b where u.dbid=b.dbid and b.name+'.'+s.name='" + segment + "'and " + "(u.segmap & power(2,s.segment)) = power(2,s.segment) group by s.segment";
            String[] rs = this.getStrings(serverName, sql1, sql2, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase segment", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql1, String sql2, String ip) throws CollectorException {
        String[] propValArr = new String[3];
        try {
            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql1);
            int low = 0;
            int totalPages = 0;
            int freePages = 0;
            float totalSize = 0.0F;
            while (rst.next()) {
                low = rst.getInt(1);
            }
            if (rst != null) {
                rst.close();
                rst = null;
            }
            rst = this.getCachedResultSet("sybase", ip, serverName, sql2);
            while (rst.next()) {
                totalPages = rst.getInt(1);
                freePages = rst.getInt(2);
                totalSize = totalPages * 1.0f * low / 1024 / 1024;
            }
            propValArr[0] = totalSize + "";
            propValArr[1] = totalPages + "";
            propValArr[2] = freePages + "";

        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }
        return propValArr;
    }

    public String[] getParamMeta() {
        //        return new String[] { "服务器名", "段名" };
        return new String[] { "ServerName", "SegmentName" };
    }

    public String[] getPropMeta() {
        //        return new String[] { "总空间大小", "总页数", "空闲页数" };
        return new String[] { "TotalSpaceSize", "TotalPages", "FreePages" };
    }
}
